<template>
    <div class="card-box">
        <!-- 半场动画钩子函数 写在标签内 -->
        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter"
        > 
            <div class="ball" v-show="ballflag" ref="ball"></div>
        </transition>  


        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunboList = 'list' :isfull="false"></swiper>
                </div>
            </div>
        </div>

        <!-- 商品购买区域 -->
        <div class="mui-card">
            <div class="mui-card-header">{{goodsinfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:<del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;
                        销售价:<span class="now_price">￥{{goodsinfo.sell_price}}</span>   
                    </p>
                    <p>购买数量:<numbox @getcount="getCarcount" :max="goodsinfo.stock_quantity"></numbox></p>
                    <p>
                        <mt-button type="primary" size="small" @click="gobuy">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="goshopcar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号:{{goodsinfo.goods_no}}</p>
                    <p>库存情况:{{goodsinfo.stock_quantity}}</p>
                    <p>上架时间:{{goodsinfo.add_time | dataFomart}}</p>    
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="godesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="gocomment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/numberbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id, //路由id
      list: [], //轮播图
      goodsinfo: {}, //商品信息
      ballflag: false, //默认小球隐藏
      carCount: 1 //默认进入页面购物车为1
    };
  },
  created() {
    this.getLunbotu(), this.getGoodsinfo();
  },
  methods: {
    getLunbotu() {
      //获取轮播图
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          result.body.message.forEach(item => {
            item.img = "https://unsplash.it/300/200/?random";
          });
          this.list = result.body.message;
        }
      });
    },
    getGoodsinfo() {
      //获取商品信息
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsinfo = result.body.message[0];
        }
      });
    },
    godesc(id) {
      //编程导航去图文页面
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    gocomment(id) {
      //编程导航去评论
      this.$router.push({ name: "goodscomment", params: { id } });
    },
    gobuy() {
      //立即购买
    },
    goshopcar() {
      //加入购物车
      this.ballflag = !this.ballflag;

      var shopcarinfo = { 
        //新建一个存入购物车的商品对象
        id: this.id,
        pirce: this.goodsinfo.sell_price,
        count: this.carCount,
        // url:this.list.img,
        title:this.goodsinfo.title,
        selected: true
      };
      
      //调用vuex中的addTocar方法,并把shopcarinfo传递过去
      this.$store.commit("addToCar",shopcarinfo)
    },
    beforeEnter(el) {
      //半场动画钩子函数
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      //半场动画钩子函数
      el.offsetWidth; //加上这句不然不现实动画

      //获取小球位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      //获取购物车位置
      const shopcarPosition = document
        .getElementById("shopcar")
        .getBoundingClientRect();
      //x坐标差
      const xDist = shopcarPosition.left - ballPosition.left;
      //y坐标差
      const yDist = shopcarPosition.top - ballPosition.top;
      // console.log(xDist, yDist);
      el.style.transform = `translate(${xDist}px,${yDist}px)`; //字符串拼接 ``
      //   console.log(el.style.transform)
      el.style.transition = "all 0.5s ease";
      done();
    },
    afterEnter(el) {
      //半场动画钩子函数
      this.ballflag = !this.ballflag;
    },
    getCarcount(carCount) {
      //父组件方法 购物车计数
      this.carCount = carCount; //子组件调用返回的值保存到这里
      //   console.log('接收到子组件传递的值'+this.carCount);
    }
  },
  components: {
    swiper, //注册轮播
    numbox //注册计数器
  }
};
</script>

<style lang="sass" scoped>
.card-box{
    background-color:#eee;
    overflow:hidden;
    .now_price{
        font-weight:bold;
        font-size:16px;
        color:red;
    }
}
.mui-card-footer{
    display:block;
    button{
        margin:10px 0;
    }
}
.ball{
    width:15px;
    height:15px;
    border-radius:50%;
    background-color:red;
    position:absolute;
    top:391px;
    left:135px;
    z-index:99;
}
</style>
